package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.GiftDetailsDto;
import com.huaxiafinance.management.dto.request.QueryGiftDto;
import com.huaxiafinance.management.dto.request.ScoreGiftDto;
import com.huaxiafinance.management.service.ScoreGiftService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@Controller("scoreGiftController")
@RequestMapping("/scoreGift")
public class ScoreGiftController {

  protected Logger logger = Logger.getLogger(ScoreGiftController.class);
  @Autowired
  private ScoreGiftService scoreGiftService;

  /**
   * 兑换列表列表
   */
  @RequestMapping(value = "/queryScoreGiftList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreGiftList(@Valid @RequestBody QueryGiftDto dto, BindingResult bindingResult) {
    logger.info("请求积分礼品列表参数：" + dto.toString());
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
    } else {
      try {
        messageResult = scoreGiftService.queryScoreGiftList(dto);
      } catch (Exception e) {
        messageResult.setFailureResult();
        logger.info("请求积分礼品异常原因：" + e);
      }
    }
    logger.info("请求积分礼品列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 新增积分礼品
   *
   * @param dto 请求参数
   */
  @RequestMapping(value = "/insertScoreGift", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String insertScoreRule(@Valid @RequestBody ScoreGiftDto dto, BindingResult bindingResult)
      throws Exception {
    logger.info("请求增加积分礼品的参数:" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
      } else {
        messageResult = scoreGiftService.insertScoreGift(dto);
      }
    } catch (Exception e) {
      logger.info("请求增加积分礼品的异常原因:" + e);
      messageResult.setFailureResult();
    }
    logger.info("请求增加积分礼品的返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查看积分礼品详情
   */
  @RequestMapping(value = "/queryScoreGift", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreGift(HttpServletRequest request) {
    logger.info("查询积分礼品请求参数：" + request.toString());
    MessageResult messageResult = new MessageResult();
    try {
      String id = request.getParameter("id");
      if (org.apache.commons.lang.StringUtils.isNotBlank(id)) {
        messageResult = scoreGiftService.queryScoreGift(Integer.parseInt(id));
      } else {
        messageResult.setFailureResult("id不能为空");
      }
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("查询积分礼品异常原因：" + e);
    }
    logger.info("查询积分礼品返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查看兑换码使用列表
   */
  @RequestMapping(value = "/queryScoreGiftCode", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreGiftCode(@Valid @RequestBody GiftDetailsDto dto, BindingResult bindingResult) {
    logger.info("查询兑换码使用列表请求参数：" + dto.toString());
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
      } else {
        messageResult = scoreGiftService.queryScoreGiftCode(dto);
      }
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("查询兑换码使用列表异常原因：" + e);
    }
    logger.info("查询兑换码使用列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查看兑换码使用详情
   */
  @RequestMapping(value = "/queryCodeDetails", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryCodeDetails(HttpServletRequest request) {
    logger.info("查询兑换码使用详情请求参数：" + request.toString());
    MessageResult messageResult = new MessageResult();
    try {
      String giftCode = request.getParameter("giftCode");
      if (giftCode != null) {
        messageResult = scoreGiftService.queryCodeDetails(giftCode.toUpperCase());
      } else {
        messageResult.setSuccessResult();
      }
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("查询兑换码使用详情异常原因：" + e);
    }
    logger.info("查询兑换码使用详情返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查询批次状态列表
   */
  @RequestMapping(value = "/queryBatchList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryBatchList() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = scoreGiftService.queryBatchList();
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("批次状态列表异常原因：" + e);
    }
    logger.info("批次状态列表返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 下载兑换码
   */
  @RequestMapping(value = "/download", produces = "application/json; charset=utf-8")
  public void download(HttpServletRequest request, HttpServletResponse response) {
    logger.info("下载兑换码请求参数：" + request.toString());
    try {
      String id = request.getParameter("id");
      scoreGiftService.download(id, response);
    } catch (Exception e) {
      logger.info("下载兑换码请求参数异常原因：" + e);
    }

  }

  /**
   * 查看所有兑换码
   */
  @RequestMapping(value = "/queryAllCode", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryAllCode(HttpServletRequest request) {
    logger.info("查询所有兑换码请求参数：" + request.toString());
    MessageResult messageResult = new MessageResult();
    try {
      String giftCodeId = request.getParameter("id");
      if (giftCodeId != null && !("").equals(giftCodeId)) {
        messageResult = scoreGiftService.queryAllCode(Integer.parseInt(giftCodeId));
      } else {
        messageResult.setSuccessResult();
      }
    } catch (Exception e) {
      messageResult.setFailureResult();
      logger.info("查询所有兑换码异常原因：" + e);
    }
    logger.info("查询所有兑换码返回参数：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

}
